package util

import (
	"encoding/json"
	"fmt"
	"strconv"
	"strings"
	"time"

	"gitee.com/shoyer/logger"
	"github.com/spf13/cast"
)

// UintToString UintToString
func UintToString(number uint) string {
	return fmt.Sprintf("%d", number)
}

// Uint32ToString Uint32ToString
func Uint32ToString(number uint32) string {
	return fmt.Sprintf("%d", number)
}

// Uint64ToString Uint64ToString
func Uint64ToString(number uint64) string {
	return fmt.Sprintf("%d", number)
}

// IntToString IntToString
func IntToString(number int) string {
	return fmt.Sprintf("%d", number)
}

// Int64ToString Int64ToString
func Int64ToString(number int64) string {
	return fmt.Sprintf("%d", number)
}

// StringToUint StringToUint
func StringToUint(str string) uint {
	if str == "" {
		return 0
	}

	var result, err = strconv.ParseUint(str, 10, 64)

	if err != nil {
		logger.Error(err.Error())
		return 0
	}

	return uint(result)
}

// StringToUint64 StringToUint64
func StringToUint64(str string) uint64 {
	if str == "" {
		return 0
	}

	var result, err = strconv.ParseUint(str, 10, 64)

	if err != nil {
		logger.Error(err.Error())
		return 0
	}

	return result
}

// StringToUint32 StringToUint32
func StringToUint32(str string) uint32 {
	if str == "" {
		return 0
	}

	var result, err = strconv.ParseUint(str, 10, 64)

	if err != nil {
		logger.Error(err.Error())
		return 0
	}

	return uint32(result)
}

// StringToInt64 StringToInt64
func StringToInt64(str string) int64 {
	if str == "" {
		return 0
	}

	var result, err = strconv.ParseInt(str, 10, 64)

	if err != nil {
		logger.Error(err.Error())
		return 0
	}

	return result
}

// StringToInt StringToInt
func StringToInt(str string) int {
	if str == "" {
		return 0
	}

	var result, err = strconv.ParseInt(str, 10, 64)

	if err != nil {
		logger.Error(err.Error())
		return 0
	}

	return int(result)
}

// StringToInt32 StringToInt32
func StringToInt32(str string) int32 {
	if str == "" {
		return 0
	}

	var result, err = strconv.ParseInt(str, 10, 64)

	if err != nil {
		logger.Error(err.Error())
		return 0
	}

	return int32(result)
}

// AnyToString 任意数据类型转为string类型
func AnyToString(v interface{}) string {
	return fmt.Sprint(v)
}

// StringToFloat32 StringToFloat32
func StringToFloat32(str string) float32 {
	if str == "" {
		return 0
	}

	var result, err = strconv.ParseFloat(str, 32)

	if err != nil {
		logger.Error(err.Error())
		return 0
	}

	return float32(result)
}

// StringToFloat64 StringToFloat64
func StringToFloat64(str string) float64 {
	if str == "" {
		return 0
	}

	var result, err = strconv.ParseFloat(str, 64)

	if err != nil {
		logger.Error(err.Error())
		return 0
	}

	return float64(result)
}

// ObjectToBytes ObjectToBytes
func ObjectToBytes(obj interface{}) (r []byte) {
	if obj == nil {
		return
	}

	r, err := json.Marshal(obj)
	logger.ErrorfIfNotNil(err)

	return
}

// ObjectToJSONString ObjectToJSONString
func ObjectToJSONString(obj interface{}) string {
	if obj == nil {
		return ""
	}

	i, err := json.Marshal(obj)
	logger.ErrorfIfNotNil(err)

	return cast.ToString(i)
}

// ObjectToJSONStringIndent ObjectToJSONStringIndent
func ObjectToJSONStringIndent(obj interface{}) string {
	if obj == nil {
		return ""
	}

	i, err := json.MarshalIndent(obj, "", "  ")
	logger.ErrorfIfNotNil(err)

	return cast.ToString(i)
}

// MapStringString2StringAny MapStringString2StringAny
func MapStringString2StringAny(src map[string]string) map[string]interface{} {
	if src == nil {
		return nil
	}

	result := map[string]interface{}{}
	for k, v := range src {
		result[k] = v
	}

	return result
}

// BoolToString BoolToString
func BoolToString(v bool) string {
	return strconv.FormatBool(v)
	// return fmt.Sprintf("%t", v)
}

// StringToBool StringToBool
func StringToBool(v string) bool {
	v = strings.TrimSpace(v)
	if v == "false" || v == "" {
		return false
	}

	return true
}

// IntToDuration IntToDuration
func IntToDuration(unit int) time.Duration {
	return time.Duration(int64(unit))
}

// DurationToInt64 DurationToInt64
func DurationToInt64(d time.Duration) int64 {
	return int64(d.Seconds())
}

// TimeToTimePointer TimeToTimePointer
func TimeToTimePointer(t time.Time) *time.Time {
	return &t
}

// TimePointerToTimestamp TimePointerToTimestamp
func TimePointerToTimestamp(t *time.Time) int64 {
	if t == nil {
		return 0
	}

	return t.Unix()
}
